package com.islewars.client.battle;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.canvas.client.Canvas;
import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.canvas.dom.client.CssColor;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.islewars.client.Calculation;
import com.islewars.client.Vector;
import com.islewars.client.dto.GameDto;
import com.islewars.client.dto.MountainDto;
import com.islewars.client.dto.PlayerDto;
import com.islewars.client.dto.TankDto;
import com.islewars.client.service.GameService;
import com.islewars.client.service.GameServiceAsync;

public class TacticalScreen {
	private Canvas canvas;
	private Context2d context;
	private static final int canvasWidth = 700;
	private static final int canvasHeight = 700;
	private List<MountainDto> mountains;
	private static GameServiceAsync gameServiceAsync = GWT.create(GameService.class);
	private final GameDto gameDto;

	public TacticalScreen(final GameDto gameDto) {
		this.gameDto = gameDto;
		init();
	}
	
	private void init() {
		HorizontalPanel mainHorizontalPanel = new HorizontalPanel();
		mainHorizontalPanel.setWidth("80%");
		mainHorizontalPanel.setHeight(Window.getClientHeight() + "px");
		((HorizontalPanel) mainHorizontalPanel).setHorizontalAlignment(HasAlignment.ALIGN_CENTER);

		canvas = Canvas.createIfSupported();

		if (canvas == null) {
			RootPanel.get().add(new Label("Sorry, your browser doesn't support the HTML5 Canvas element"));
			return;
		}

		canvas.setStyleName("mainCanvas");
		canvas.setWidth(canvasWidth + "px");
		canvas.setCoordinateSpaceWidth(canvasWidth);

		canvas.setHeight(canvasHeight + "px");
		canvas.setCoordinateSpaceHeight(canvasHeight);

		mainHorizontalPanel.add(canvas);

		context = canvas.getContext2d();

		mountains = new ArrayList<MountainDto>();

		VerticalPanel verticalPanel = new VerticalPanel();

		mainHorizontalPanel.add(verticalPanel);

		RootPanel.get().setStyleName("rootStyle");
		RootPanel.get().clear();
		RootPanel.get().add(mainHorizontalPanel);

		context.setFillStyle(CssColor.make(0, 160, 0));
		
		draw();
	}

	private void draw() {
		for (int x = 0; x < canvasWidth; x += 10) {
			for (int y = 0; y < canvasHeight; y += 10) {
				double height = Calculation.getHeight(new Vector(x, y), mountains);
				context.setFillStyle(CssColor.make((int) height/2, 160, 0));
				context.fillRect(x, y, 10, 10);
			}
		}
		
		System.out.println("gameDto.players.size(): " + gameDto.players.size());
		
		for (PlayerDto playerDto : gameDto.players) {
			System.out.println("playerDto.name: " + playerDto.name);
			for (TankDto tankDto : playerDto.tankDtos) {
				System.out.println("tankDto.position: " + tankDto.position);
				context.setFillStyle(CssColor.make(200, 0, 0));
				context.fillRect(tankDto.position.x, tankDto.position.y, 30, 30);
			}
		}
	}
}
